package com.example.administrator.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;

import com.example.administrator.myapplication.media.Music;
import com.example.administrator.myapplication.biz.ScoreBiz;
import com.example.administrator.myapplication.util.ToastUtil;

public class SettingActivity extends Activity implements View.OnClickListener{
    private SeekBar mSeekBar;
    private int mVolume;
    private ImageView mIsMusicStart;
    private ScoreBiz mScoreBiz;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        mScoreBiz = new ScoreBiz(this);
        mIsMusicStart = (ImageView) findViewById(R.id.is_volume_on);
        //打开
        findViewById(R.id.volume_on).setOnClickListener(this);
        //关闭
        findViewById(R.id.volume_off).setOnClickListener(this);
        //调大
        findViewById(R.id.volume_down).setOnClickListener(this);
        //调小
        findViewById(R.id.volume_up).setOnClickListener(this);
        //返回
        findViewById(R.id.bt_back_menu).setOnClickListener(this);
        //清空存档
        findViewById(R.id.bt_reset_rank).setOnClickListener(this);

        mSeekBar = (SeekBar) findViewById(R.id.sb_volume);
        mSeekBar.setOnSeekBarChangeListener(onSeekBarChangeListener);
        mVolume = Music.Curr_VOLUME;
        mSeekBar.setMax(Music.MAX_VOLUME);
        mSeekBar.setProgress(mVolume);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.volume_on:
                Music.isMusicOn = true;
                Music.startMusic(this,true,R.raw.play);
                mIsMusicStart.setImageResource(R.drawable.audio_on);

                break;
            case R.id.volume_off:
                Music.stopMusic();
                mIsMusicStart.setImageResource(R.drawable.audio_off);
                Music.isMusicOn = false;
                break;
            case R.id.volume_down:
                if(mVolume>0){
                    mVolume--;
                }
                mSeekBar.setProgress(mVolume);
                Music.setVolume(mVolume);
                break;
            case R.id.volume_up:
                if(mVolume<Music.MAX_VOLUME){
                    mVolume++;
                }
                mSeekBar.setProgress(mVolume);
                Music.setVolume(mVolume);
                break;
            case R.id.bt_back_menu:
                onBackPressed();
                break;
            case R.id.bt_reset_rank:
                  mScoreBiz.deleteAll();
                ToastUtil.show(this,"清空存档成功！");
                break;
        }
    }
    private SeekBar.OnSeekBarChangeListener onSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            Music.setVolume(progress);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    };
}
